﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Douban.Modules.Conversation.Views;
using System.Collections.ObjectModel;
using Douban.Modules.Conversation.Models;
using System.Windows.Input;
using Microsoft.Practices.Composite.Presentation.Commands;
using Douban.Api;
using Douban.Core;

namespace Douban.Modules.Conversation.ViewModels
{
    public class ChatLogViewModel : ObservableObject, IChatLogViewModel
    {
        ChatLogModel _chatLog;
        private IList<string> _dateFiles;
        private IList<MessageModel> _todayMessages;
        private string _date;
        private string _pageNumber;
        private string _pageCount;
        private int _index;
        private ICommand _showFirstCommand;
        private ICommand _showPreviousCommand;
        private ICommand _showNextCommand;
        private ICommand _showLastCommand;

        public ChatLogViewModel(IChatLogView view)
        {
            TodayMessages = new List<MessageModel>();
            DisplayedMessages = new ObservableCollection<MessageModel>();
            View = view;
            View.Context = this;
        }

        #region IChatLogViewModel Members

        public IChatLogView View { get; set; }

        public ChatLogFileManager FileManager { get; set; }

        public IList<MessageModel> TodayMessages
        {
            get
            {
                if (_chatLog != null && _todayMessages.Count == 0)
                {
                    foreach (var message in _chatLog.Messages)
                    {
                        _todayMessages.Add(message);
                    }
                }
                return _todayMessages;
            }
            set { _todayMessages = value; }
        }

        public ObservableCollection<MessageModel> DisplayedMessages { get; set; }

        public string Date
        {
            get { return _date; }
            set
            {
                if (_date != value)
                {
                    _date = value;
                    NotifyPropertyChanged("Date");
                }
            }
        }

        public string PageNumber
        {
            get { return _pageNumber;}
            set
            {
                if (_pageNumber != value)
                {
                    _pageNumber = value;
                    NotifyPropertyChanged("PageNumber");
                }
            }
        }

        public string PageCount
        {
            get { return _pageCount; }
            set
            {
                if (_pageCount != value)
                {
                    _pageCount = value;
                    NotifyPropertyChanged("PageCount");
                }
            }
        }

        public ICommand ShowFirstCommand
        {
            get
            {
                if (_showFirstCommand == null)
                    _showFirstCommand = new DelegateCommand<object>(obj => ShowFirstChatLog());

                return _showFirstCommand;
            }
        }

        public ICommand ShowPreviousCommand
        {
            get
            {
                if (_showPreviousCommand == null)
                    _showPreviousCommand = new DelegateCommand<object>(obj => ShowPreviousChatLog());

                return _showPreviousCommand;
            }
        }

        public ICommand ShowNextCommand
        {
            get
            {
                if (_showNextCommand == null)
                    _showNextCommand = new DelegateCommand<object>(obj => ShowNextChatLog());

                return _showNextCommand;
            }
        }

        public ICommand ShowLastCommand
        {
            get
            {
                if (_showLastCommand == null)
                    _showLastCommand = new DelegateCommand<object>(obj => ShowLastChatLog());

                return _showLastCommand;
            }
        }

        public void Run()
        {

            Date = DateTime.Now.ToString("yyyy-MM-dd");

            _chatLog = FileManager.GetChatLog(Date);

            if (_dateFiles == null)
                _dateFiles = FileManager.ChatLogDateFiles;

            _index = _dateFiles.Count - 1;

            RefreshDisplayedMessage(_index);
        }

        #endregion

        private void ShowFirstChatLog()
        {
            if (_index == 0) return;

            _index = 0;

            Date = _dateFiles[_index];

            _chatLog = FileManager.GetChatLog(Date);

            RefreshDisplayedMessage(_index);
        }

        private void ShowPreviousChatLog()
        {
            _index = _dateFiles.IndexOf(_date);

            if (_index == 0) return;

            Date = _dateFiles[--_index];

            _chatLog = FileManager.GetChatLog(Date);

            RefreshDisplayedMessage(_index);
        }

        private void ShowNextChatLog()
        {
            _index = _dateFiles.IndexOf(_date);

            if (_index == _dateFiles.Count - 1) return;

            Date = _dateFiles[++_index];

            _chatLog = FileManager.GetChatLog(Date);

            RefreshDisplayedMessage(_index);
        }

        private void ShowLastChatLog()
        {
            if (_index == _dateFiles.Count - 1) return;

            _index = _dateFiles.Count - 1;

            Date = _dateFiles[_index];

            _chatLog = FileManager.GetChatLog(Date);

            RefreshDisplayedMessage(_index);
        }

        private void RefreshDisplayedMessage(int index)
        {
            PageNumber = (index + 1).ToString();

            PageCount = _dateFiles.Count.ToString();

            DisplayedMessages.Clear();

            if (_chatLog == null) return;

            foreach (var message in _chatLog.Messages)
            {
                DisplayedMessages.Add(message);
            }

            View.SetChatLogScrollToBottom();
        }
    }
}
